// pages/edit/edit.js
var app = getApp();
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: [],
        index:0,
		ischecked:0,
		buidindex:-1,
		florrindex:-1,
		roomindex:-1,
		address_info:""

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;

        if(options.address_id != undefined){
            // 获取地址信息
            wx.setNavigationBarTitle({
                title: '编辑地址'
            })
            utils.showLoading();
            that.setData({
                address_id:options.address_id,
            });
            utils.http("/api/v1/address/address_info", {
                address_id:that.data.address_id
            }, function (res) {
                if (res.state == 1) {
                    that.setData({
                        address_info:res.datas,
                        school_id: res.datas.school_id,
                        building_id:res.datas.building_id,
                        school_name:res.datas.school_name,
                        ischecked:res.datas.is_default
                    })
                    that.schoolBuindingList();
					// that.getFloorList();
					// that.getroomList();
					
                } else {
                    utils.hideLoading();
                    utils.message(res.datas)
                }
            })
        }else{
            that.setData({
                school_name:wx.getStorageSync("schoolInfo").schooltitle,
                school_id:wx.getStorageSync("schoolInfo").schoolid
            })
          
        }
		 that.schoolBuindingList();
    },
    //根据学校获取该学校的楼宇 用于添加或者编辑地址
    schoolBuindingList: function () {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/address/get_school_buinding", {
            school_id:that.data.school_id
        }, function (res) {
            if (res.state == 1) {
				utils.hideLoading();
				that.setData({
					building_list: res.datas,
				})
				if (that.data.address_info!=""){
					for (var i in res.datas) {
						if (res.datas[i].id == that.data.address_info.building_id) {
							that.setData({
								buidindex: i
							})
						
						}
					}
					that.getFloorList();
				}
				
               
				
               
            } else {
                utils.hideLoading();
                utils.message(res.datas)
            }
        })
    },
    bindPickerChange:function(e){
        var that = this;
        that.setData({
            buidindex:e.detail.value
        });
		that.getFloorList();
    },

	//根据楼宇获取楼层
	getFloorList:function(e){
		var that = this;

		utils.http("/api/v1/address/get_floor_list",{
			build_id: that.data.building_list[that.data.buidindex].id
		},function(res){
			utils.hideLoading();
			//console.log(res);
			that.setData({
				floorlist: res.datas
			})
			if (that.data.address_info != "") {
				for (var i in res.datas) {
					if (res.datas[i].id == that.data.address_info.floor_id) {
						that.setData({
							florrindex: i
						})
					}
				}
				that.getroomList();

			}
			

		})
	},
	bindFlorrChange:function(e){
		console.log(e);
		var that=this;
		that.setData({
			florrindex: e.detail.value
		});
		that.getroomList();
	},

	//根据楼层获取宿舍号
	getroomList: function (e) {
		var that = this;

		utils.http("/api/v1/address/get_room_list", {
			floor_id: that.data.floorlist[that.data.florrindex].id
		}, function (res) {
			utils.hideLoading();
			that.setData({
				roomlist: res.datas
			})
			if (that.data.address_info != "") {
				for (var i in res.datas) {
					if (res.datas[i].id == that.data.address_info.room_id) {
						console.log(i)
						that.setData({
							roomindex: i
						})
					}
				}

			}
			


		})
	},
	//
	bindroomChange:function(e){
		var that = this;
		that.setData({
			roomindex: e.detail.value
		});
	},
    switch1Change:function(e){
        var that = this;
        console.log(e);
        that.setData({
            ischecked : e.detail.value == true ? 1 : 0,
        });
    },
    formSubmit:function(e){
        var that = this;
		console.log(that.data.address_id);
		
        var name = e.detail.value.name;
        var mobile = e.detail.value.mobile;
        var detail = e.detail.value.detail;
        if(name.length < 1){
            utils.showTishi('请输入姓名');
            return false;
        }
        if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(mobile))) {
            utils.message('请输入正确的11位手机号码');
            return false;
        }
		if (that.data.buidindex==-1){
			utils.message('请选择宿舍楼');
			return false;
		}
		if (that.data.florrindex == -1) {
			utils.message('请选择楼层');
			return false;
		}
		if (that.data.roomindex == -1) {
			utils.message('请选择宿舍号');
			return false;
		}
        // if (detail.length < 1) {
        //     utils.message('请填写详细地址');
        //     return false;
        // }

        utils.showLoading();
		var api = ""
		if (that.data.address_id == undefined) {
			api = {
				name: name,
				mobile: mobile,
				school_id: that.data.school_id,
				building_id: that.data.building_list[that.data.buidindex].id,
				floor_id: that.data.floorlist[that.data.florrindex].id,
				room_id:that.data.roomlist[that.data.roomindex].id,
				is_default: that.data.ischecked,
			}
		} else {
			api = {
				address_id: that.data.address_id,
				name: name,
				mobile: mobile,
				school_id: that.data.school_id,
				building_id: that.data.building_list[that.data.buidindex].id,
				floor_id: that.data.floorlist[that.data.florrindex].id,
				room_id: that.data.roomlist[that.data.roomindex].id,
				is_default: that.data.ischecked
			}
		}
		var apiUrl = '/api/v1/address/add_modify_address_v2';
		utils.http(apiUrl, api, function (res) {
            utils.hideLoading();
            if(res.state==1){
                utils.showSuccess('操作成功');
                setTimeout(function(){
                    wx.navigateBack();
                },2000);
            }else{
                utils.message(res.datas);
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})